package co.yixiang.modules.activity.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.activity.domain.YxStoreCouponProduct;
import co.yixiang.modules.activity.service.YxStoreCouponProductService;
import co.yixiang.modules.activity.service.dto.YxStoreCouponProductQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Walert
* @date 2020-03-06
*/
@Api(tags = "优惠券商品接口管理")
@RestController
@RequestMapping("/api/yxStoreCouponProduct")
public class YxStoreCouponProductController {

    private final YxStoreCouponProductService yxStoreCouponProductService;

    public YxStoreCouponProductController(YxStoreCouponProductService yxStoreCouponProductService) {
        this.yxStoreCouponProductService = yxStoreCouponProductService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','yxStoreCouponProduct:list')")
    public void download(HttpServletResponse response, YxStoreCouponProductQueryCriteria criteria) throws IOException {
        yxStoreCouponProductService.download(yxStoreCouponProductService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询优惠券商品接口")
    @ApiOperation("查询优惠券商品接口")
    @PreAuthorize("@el.check('admin','yxStoreCouponProduct:list')")
    public ResponseEntity<Object> getYxStoreCouponProducts(YxStoreCouponProductQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxStoreCouponProductService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增优惠券商品接口")
    @ApiOperation("新增优惠券商品接口")
    @PreAuthorize("@el.check('admin','yxStoreCouponProduct:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxStoreCouponProduct resources){
        Long addTime = System.currentTimeMillis()/1000L;
        resources.setAddTime(addTime.intValue());
        resources.setIsDel(0);
        return new ResponseEntity<>(yxStoreCouponProductService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改优惠券商品接口")
    @ApiOperation("修改优惠券商品接口")
    @PreAuthorize("@el.check('admin','yxStoreCouponProduct:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxStoreCouponProduct resources){
        yxStoreCouponProductService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除优惠券商品接口")
    @ApiOperation("删除优惠券商品接口")
    @PreAuthorize("@el.check('admin','yxStoreCouponProduct:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        yxStoreCouponProductService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}